<template>
  <el-form
    ref="systemForm"
    :model="formData"
    :rules="formRules"
    label-width="90px"
    label-position="right"
  >
    <el-form-item v-if="action === 'edit'" prop="id">
      <input v-model="formData.id" type="hidden">
    </el-form-item>
    <el-form-item label="名称" prop="sysName">
      <el-input v-model="formData.sysName" type="text" placeholder="请输入名称" />
    </el-form-item>
    <el-form-item label="排序" prop="seq">
      <el-input-number v-model="formData.seq" :max="99" :min="0" style="width: 100%;" />
    </el-form-item>
    <el-form-item label="描述" prop="sysDesc">
      <el-input v-model="formData.sysDesc" type="textarea" :autosize="{ minRows: 3}" placeholder="请输入系统简介" :maxlength="255" />
    </el-form-item>
  </el-form>
</template>
<script>
import { get, save, update } from '@/api/admin/system';

export default {
  name: 'SystemFormDialog',
  props: {
    action: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      formRules: {
        sysName: [
          { required: true, message: '名称不能为空！', trigger: 'blur' }
        ]
      },
      formData: {
        id: null,
        sysName: null,
        sysDesc: null,
        seq: 0
      }
    };
  },
  methods: {
    getInfo(id) {
      get(id).then(response => {
        const result = response.data;
        if (result.code === 0) {
          this.formData = result.data;
        } else {
          this.$notify.error({
            title: result.code,
            message: result.msg
          });
        }
      });
    },
    handleSubmit() {
      this.$refs.systemForm.validate((valid) => {
        if (valid) {
          if (this.action === 'add') {
            save(this.formData).then(response => {
              this.handleResult(response.data);
            });
          } else if (this.action === 'edit') {
            update(this.formData).then(response => {
              this.handleResult(response.data);
            });
          }
        }
      });
    },
    handleResult(obj) {
      if (obj.code === 0) {
        this.$notify({
          title: '提示',
          message: obj.msg,
          type: 'success'
        });
        this.$parent.$parent.customFormInitOption.showModal = false;
        this.reset();
        this.$parent.$parent.$refs.customTable.query();
      } else {
        this.$notify({
          title: '提示',
          message: obj.msg,
          type: 'error'
        });
      }
    },
    reset() {
      this.$refs.systemForm.resetFields();
    }
  }
};
</script>
